<?php

	class Madmin extends CI_Model {
		function __construct() {
			parent::__construct();
		}
		
		//获取用户列表
		function get_user_list() 
		{
			$query = $this->db->query("select * from user");
			return $query->result();
		}
		
		//加入用户
		function insert_user()
		{
			$username = $this->input->post('username');
			$password = $this->input->post('password');
			$this->db->query("insert into user (username, password, regtime) values ('$username', '$password', now())");
			return $this->db->affected_rows();
		}
		
		//获取单个用户信息
		function get_user($userid)
		{
			$query = $this->db->query("select * from user where id=$userid");
			return $query->result();
		}
		
		//判断用户是否存在
		function get_user_nums($username)
		{
			$query = $this->db->query("select count(*) as nums from user where username='$username'");
			$arr_result = array();
			$arr_result = $query->result();
			return $arr_result[0]->nums;
		}
		
		//删除用户
		function del_user($userid)
		{
			$this->db->query("delete from user where id=$userid");
			return $this->db->affected_rows();
		}
		
		//==================================courseware表管理===============================================================================
		//课件列表
		function get_courseware_list()
		{
			$query = $this->db->query("select courseware.*,category.name as category_name from courseware,category where courseware.category=category.id order by courseware.id desc");
			return $query->result();
		}
		
		//课件内容
		function get_courseware($coursewareid)
		{
			$query = $this->db->query("select * from courseware where id=$coursewareid");
			return $query->result();
		}
		
		//增加课件
		function insert_courseware($data)
		{
			$name = $this->input->post("name");
			$category = $this->input->post("category");
			$this->db->query("insert into courseware (name,category,filename,attachment,uploader,uploadtime,lastmodier) values ('$name','".$category."','".$data['course_filename']."','".$data['attachment_filename']."','',now(),'')");
			return $this->db->affected_rows();
		}
		
		function del_courseware($courseid)
		{	
			//删除测试答案		
			$this->db->query("delete from exam_answer where examid in (select id from exam where coursewareid = $courseid)");
			//删除测试题目
			$this->db->query("delete from exam where coursewareid = $courseid");
			//删除课件
			$this->db->query("delete from courseware where id = $courseid");			
			return $this->db->affected_rows();
		}
		
		//==================================用户登录=======================================================================
		function check_login()
		{
			$username= $this->input->post("username");
			$password= $this->input->post("password");
			$query = $this->db->query("select count(*) as nums from user where username='$username' && password='$password'");
			$arr_result = array();
			$arr_result = $query->result();
			if ($arr_result[0]->nums == 1)	return true;
				else return false;
		}
		
		//=============================================课程测验========================================================
		function get_exam($coursewareid)
		{
			$query = $this->db->query("select * from exam where coursewareid=$coursewareid");
			return $query->result();
		}
		
		function get_exam_answer($examid)
		{
			$query = $this->db->query("select * from exam_answer where examid=$examid");
			return $query->result();
		}
		
		function add_exam()
		{
			$question   = $this->input->post('question');
			$answer_a	= $this->input->post('answer_a');
			$isright_a = ($this->input->post('isright_a'))? 1 : 0;
			$answer_b	= $this->input->post('answer_b');
			$isright_b = ($this->input->post('isright_b'))? 1 : 0;
			$answer_c	= $this->input->post('answer_c');
			$isright_c = ($this->input->post('isright_c'))? 1 : 0;
			$answer_d	= $this->input->post('answer_d');
			$isright_d = ($this->input->post('isright_d'))? 1 : 0;
			
			$this->db->query("insert into exam (coursewareid, question) values (".$this->input->post('coursewareid').",'".$this->input->post('question')."')");
			$examid = $this->db->insert_id();
			if ($answer_a)	$this->db->query("insert into exam_answer (examid,answer,isright) values ($examid,'$answer_a',$isright_a)");
			if ($answer_b)	$this->db->query("insert into exam_answer (examid,answer,isright) values ($examid,'$answer_b',$isright_b)");
			if ($answer_c) 	$this->db->query("insert into exam_answer (examid,answer,isright) values ($examid,'$answer_c',$isright_c)");
			if ($answer_d)	$this->db->query("insert into exam_answer (examid,answer,isright) values ($examid,'$answer_d',$isright_d)");
			
			if ($examid)	return true;
				else	return false;
		}
		
		function del_exam($examid)
		{
			$this->db->query("delete from exam_answer where examid=$examid");
			$this->db->query("delete from exam where id=$examid");
			return $this->db->affected_rows();
		}
		
		function del_answer($answerid)
		{
			$this->db->query("delete from exam_answer where id=$answerid");
			return $this->db->affected_rows();
		}
		
		//============================类别管理==============================================
		function add_category()
		{
			$this->db->query("insert into category (name) values ('".$this->input->post("category")."')");
			return $this->db->affected_rows();
		}
		
		function get_category()
		{
			$query = $this->db->query("select * from category");
			return $query->result();
		}
		
		//=====================判断是否测试正确============================
		function do_exam($examid,$answer)
		{
			$sql_r = "";
			for ($i=0; $i<count($answer); $i++)
			{
				$sql_r .= " && id=".$answer[$i];
			}
			$query = $this->db->query("select count(*) as nums from exam_answer where examid=$examid && isright=1".$sql_r);
			return $query->result();
		}
		
		//============================添加回家作业======================================
		function insert_homework($data)
		{
			$this->db->query("insert into homework (coursewareid,filename,rightnum) values (".$data["coursewareid"].", '".$data["homework_filename"]."',".$data['rightnum'].")");
			return $this->db->affected_rows();
		}
		
		function get_homework_list($coursewareid)
		{
			$query = $this->db->query("select * from homework where coursewareid=$coursewareid");
			return $query->result();
		}
		
		function del_homework($homeworkid)
		{
			$this->db->query("delete from homework where id=$homeworkid");
			return $this->db->affected_rows();
		}
		
		function get_homework($data)
		{
			$query = $this->db->query("select rightnum from homework where coursewareid=".$data["coursewareid"]." order by rightnum");
			$result = $query->result();
			
			$arr_rightnum = array();
			foreach ($result as $row)
			{
				array_push($arr_rightnum, $row->rightnum);
			}

			if ( in_array($data["rightnum"],$arr_rightnum) )
			{	
				$rightnum = $data["rightnum"];
			} else {
				array_push($arr_rightnum,$data["rightnum"]);
				sort($arr_rightnum);		
				$key = array_search($data["rightnum"],$arr_rightnum);
				if ( $key == 0 )	$rkey = $rkey+1;
					else	$rkey = $key-1;
				$rightnum = $arr_rightnum[$rkey];
			}
			$query = $this->db->query("select * from homework where coursewareid=".$data["coursewareid"]." && rightnum = ". $rightnum ." ");			
			return $query->result();			
		}
	}